<?php

class SchoolsController extends Controller
{
	public $layout='//layouts/admin/main';

	public function init()
	{
			Yii::app()->user->loginUrl = array('/admin/site/login');
			parent::init();
	}

	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('login','logout'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update', 'index', 'view', 'cciht', 'delete', 'approve', 'decline', 'schoolmember'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

  public function actionUpdate()
  {
    if(isset($_POST['School']))
    {
      $data = $_POST['School'];
      $user_data = $_POST['Users'];
      
      $schools = School::model()->findAll('user_id = :user_id', array(':user_id' => $data['user_id']));
      
      $user = Users::model()->find('id = :id', array(':id' => $data['user_id']));
      $user->attributes = $user_data;
      $user->save();

      foreach($schools as $school)
      {
        $school->hq = $data['hq'];
        $school->save();
      }

      Yii::app()->user->setFlash('success', 'Success');
      Yii::app()->user->setFlash('content', 'You have successfully update user info');
      $this->redirect(bu('admin/users/schoolmember?id='.$data['user_id']));
    }
  }
}
